Ccay.Module.define("Ccay.example.functionDemo.roleManage.GrantOperation2Role",function(page,$S){
    var resourceTypeList ;
	page.init=function(roleId){
		initRole(roleId);
	}
	/**
	 * @description 初始化父元素
	 */
	function initRole(roleId){
		//调用dwr的lookup服务，查询数据
		Ccay.doGet("services/ccay/security/role/single/"+roleId,'',function(role){
			    $S("#roleId").val(role.roleId);
				$S("#roleName").text(role.roleName);
				$S("#roleDesc").text(role.roleDesc);
				initOperationTabs();
		});
	
	};
	//初始化资源类型table页
	function initOperationTabs(){
		Ccay.doGet("services/ccay/security/operation/list",'',function(resourceTypes){
			resourceTypeList = resourceTypes;
		 	for(var i = 0;i<resourceTypes.length;i++){
		 		if(i==0){
		 			$S('#operationTabs-0').attr("title",resourceTypes[i]);
		 			$S('#operationTabs-0-body').html("<table>" +
			 				"<td style=\"padding: 0\"><ul id=\"ccayOperationtree-"+i+"\" class=\"ztree\"></ul></td>" +
			 				"<td style=\"padding: 0\"><ul id=\"roleOperation-"+i+"\" class=\"ztree\"></ul></td>" +
			 				"</table>");
		 			page.loadResourceOperations(resourceTypes[0],0);
		 			$S("#currentIndex").val(0); 
		 			$S("#currentResourceType").val(resourceTypes[0]);
			 	}else{
			 		$S('#operationTabs').append("<div id=\"operationTabs-"+i+"\" title=\""+resourceTypes[i]+" \">");
			 		$S('#operationTabs-'+i).html("<div>" + $S('#tabledivButtons').html() + "</div>");
			 		$S('#operationTabs-'+i).append("<div id=\"operationTabs-"+i+"-body\"></div>");
			 		$S('#operationTabs-'+i+'-body').html("<table>" +
			 				"<td style=\"padding: 0\"><ul id=\"ccayOperationtree-"+i+"\" class=\"ztree\"></ul></td>" +
			 				"<td style=\"padding: 0\"><ul id=\"roleOperation-"+i+"\" class=\"ztree\"></ul></td>" +
			 				"</table>");
			 		page.loadResourceOperations(resourceTypes[i],i);
			 		//$S('#operationTabs').ccayTab().find('.l-tab-links li')[i].click(page.changResourceOperations(resourceTypes[i],i););
			 	}
		 	}
		 	$S('#operationTabs').ccayTab();
		 	$S("#tabledivButtons").show();
		 	$S('#operationTabs').ccayTab().find('.l-tab-links li').click(function(){
		 			var index = this.tabid.substring(7,this.tabid.length)-1;
		 			page.changResourceOperations(resourceTypes[index],index);	
		 	});
		});
	};
	/**
	 * 选择tab页 设置变量
	 */
	page.changResourceOperations = function (resourceType,indx){
		$S("#currentIndex").val(indx); 
		$S("#currentResourceType").val(resourceType);
	}
	/**
	 * 根据资源类型 加载权限功能点树
	 */
   page.loadResourceOperations = function (resourceType,indx){
   		 var roleId = $S("#roleId").val();
   		// $S("#currentIndex").val(indx); 
		// $S("#currentResourceType").val(resourceType);
		  /**
		  * 获取角色具有的权限
		  */
		  Ccay.doGet("services/ccay/security/operation/list/"+roleId+"/"+resourceType,'',function(resources){
   	     	var liStr = "<li>"+$i18n("ccay.role.message.currentUserPermissions")+"</li>";
   	     	if(resources!=null){
   	     		for(var i =0;i<resources.length;i++){
   	     			var operations = resources[i]["operations"];
   	     			if(operations == null){
   	     			 	break;
   	     			}
   	     			liStr += "<li class=\"bold\">"+resources[i]["resourceDesc"].encode()+"</li>";
   	     			liStr += "<ul>";
   	     			for(var j=0;j<operations.length;j++){
   	     				liStr += "<li>&nbsp;&nbsp;&nbsp;"+operations[j]["operationDesc"].encode()+"</li>";
   	     			}
   	     			liStr += "</ul>";
   	     			
   	     		}
   	     	}
   	     	$S('#roleOperation-'+indx).html(liStr)
   	     	page.findResourceList(resourceType,indx,resources);
   	     	
   	     });	
   }
  	/** * 获取权限列表 绘制树
   	      */
   page.findResourceList = function(resourceType,indx,roleResources){
	   Ccay.doGet("services/ccay/security/operation/list/tree/"+resourceType,'',function(resources){
  	 	 	var setting = {
				    check: {
						enable: true
					},
				   data: {
						key: {
							name: "rpDesc",
							children: "rpChildren",
							title: "rpDesc"
						},
						simpleData: {
							enable: false,
							idKey: "rpCode",
							pIdKey: "rpCode",
							rootPId: null
						}
				   }
				
			};
			//父子选择是否联动
			setting.check.chkboxType= { "Y" : "ps", "N" : "ps" };
			var treeObject = Ccay.tree.init($("#ccayOperationtree-"+indx), setting, resources,false,null);
			page.checkOperationDefinitions(roleResources,treeObject);
			
	});
   		
   }
   /**
    * 设置选择状态  父没有关联起来  
    */
   page.checkOperationDefinitions = function(roleResources,treeObject){
	   	if(roleResources == null || roleResources.length<1 ){
	   		return;
	   	}
	   	for(var i=0;i<roleResources.length;i++){
   			for(var ii=0;ii<roleResources[i]["operations"].length;ii++){
   					var nodes = treeObject.getNodesByParam("rpId",roleResources[i]["operations"][ii]["operationId"]);
   					if(nodes !=null && nodes.length>0){
   						for(var j=0;j<nodes.length;j++){
   							treeObject.checkNode(nodes[j],true,true);
   						}
   					}

   			}
	   	}
   }

    /**
    * 删除无用资源
    */
   page.deleteUselessOperationDefinitions = function(){
	   Ccay.UI.confirm($i18n('ccay.common.confirm.sureDelete'),function(foo){
		   if(foo){
			  Ccay.UI.showMessage('"删除无效资源"：为演示Demo！','question');
		   }
	   });
   }
  
});